package net.zoneland.knowledge.config;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * .
 *
 * @author: bcc
 * 2021/3/15
 */
@Configuration
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Profile({"hz-test", "prod", "zmcc-zh-prod", "zmcc-zl-prod","zmcc-dz-prod"})
public class RedisConfigCluster {

    private String nodes;

    private Integer timeout;

    private String maxRedirects;

    private String password;

    @Bean
    public RedisConnectionFactory myLettuceConnectionFactory() {
        Map<String, Object> source = new HashMap<>();
        source.put("spring.redis.cluster.nodes", nodes);
        source.put("spring.redis.cluster.timeout", timeout);
        source.put("spring.redis.cluster.max-redirects", maxRedirects);
        MapPropertySource mapPropertySource = new MapPropertySource("RedisClusterConfiguration", source);
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(mapPropertySource);
        redisClusterConfiguration.setPassword(password);
        return new LettuceConnectionFactory(redisClusterConfiguration);
    }

    @Bean
    public RedisTemplate<String, Serializable> redisTemplate() {
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(genericJackson2JsonRedisSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        template.setConnectionFactory(myLettuceConnectionFactory());
        return template;
    }

    /**
     * .
     *
     * @return nodes
     */
    public String getNodes() {
        return nodes;
    }

    /**
     * .
     *
     * @param nodes to set
     */
    public void setNodes(final String nodes) {
        this.nodes = nodes;
    }

    /**
     * .
     *
     * @return timeout
     */
    public Integer getTimeout() {
        return timeout;
    }

    /**
     * .
     *
     * @param timeout to set
     */
    public void setTimeout(final Integer timeout) {
        this.timeout = timeout;
    }

    /**
     * .
     *
     * @return maxRedirects
     */
    public String getMaxRedirects() {
        return maxRedirects;
    }

    /**
     * .
     *
     * @param maxRedirects to set
     */
    public void setMaxRedirects(final String maxRedirects) {
        this.maxRedirects = maxRedirects;
    }

    /**
     * .
     *
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * .
     *
     * @param password to set
     */
    public void setPassword(final String password) {
        this.password = password;
    }
}
